/*********************************************************************************
Class Name: NMROpptyAmountUpdateCls
Author: Accenture IDC
Date:4/23/2012 
Requirement/Project Name: NMR  
Requirement Description:  Update Opportunity Amount as per Override Amount Update
Version History: Created
**********************************************************************************/
Public Class NMROpptyAmountUpdateCls{
    //Method for carrying out the amount update logic on opportunity
    Public static void updateOppty(Map<Id,Opportunity> OpptyMap){
        Map<Id,Proposal_Summary__c> proposalMap = new Map<Id,Proposal_Summary__c>();//Map to hold proposals against oppty ids
        Map<Id,Contract_Summary__c> contractMap = new Map<Id,Contract_Summary__c>();//Map to hold contracts against oppty ids
        proposalMap = getProposals(OpptyMap.keySet());//getting the proposal maps
        contractMap = getContracts(OpptyMap.keySet());//getting the contract maps
        for(id oppid:OpptyMap.keySet()){
            if(!contractMap.containsKey(oppid)){//amount to be updated only for oppties without contracts
                if(OpptyMap.get(oppid).Override_Amount__c != NULL){//update amount with override amount if override amount is not null
                    OpptyMap.get(oppid).Amount = OpptyMap.get(oppid).Override_Amount__c;
                    }
                 //else update the Proposal_NISD_Amount__c from the respective proposal
                 else if(OpptyMap.get(oppid).Override_Amount__c == NULL && proposalMap.containsKey(oppid) && proposalMap.get(oppid).Proposal_NISD_Amount__c != NULL){
                    OpptyMap.get(oppid).Amount = proposalMap.get(oppid).Proposal_NISD_Amount__c;
                 }
            }
        }
    }
    //method to query proposals for oppties
    Public static map<Id,Proposal_Summary__c> getProposals(Set<Id> OpptyIds){
        map<Id,Proposal_Summary__c> propMap = new map<Id,Proposal_Summary__c>();
        for(Proposal_Summary__c propSuammary:[select id,Opportunity_Id__c,Proposal_NISD_Amount__c from Proposal_Summary__c where Opportunity_Id__c IN: OpptyIds]){
            propMap.put(propSuammary.Opportunity_Id__c,propSuammary);
        }
        return propMap;
    }
    //method to query contracts for oppties
    Public static map<Id,Contract_Summary__c> getContracts(Set<Id> OpptyIds){
        map<Id,Contract_Summary__c> conMap = new map<Id,Contract_Summary__c>();
        for(Contract_Summary__c conSuammary:[select id,Opportunity_Id__c,Contract_NISD_Amount__c from Contract_Summary__c where Opportunity_Id__c IN: OpptyIds]){
            conMap.put(conSuammary.Opportunity_Id__c,conSuammary);
        }
        return conMap;
    }
}